const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  /**
   * index action
   * @return {Promise} []
   */
  async listAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get()
    delete query.page
    delete query.size

    if(query.company_name){
      query.company_name = ['like', `%${query.company_name}%`]
    }

    if(query.name){
      query.name = ['like', `%${query.name}%`]
    }

    if(query.tag){
      query.tag = ['like', `%${query.tag}%`]
    }

    if(query.identityTag){
      query.identityTag = ['like', `%${query.identityTag}%`]
    }

    const model = this.model('figure');
    const data = await model.order(['id DESC']).where(query).page(page, size).countSelect();

    return this.success(data);
  }
  async getSingleAction(){
    const id = this.get('id')

    const model = this.model('figure');
    const data = await model.where({id: id}).find();

    return this.success(data);

  }
  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('figure');

    if (id > 0) {
      if(values.phone || values.weixin){
        values.has_contact = 1
      }

      if(values.identityTag.length && think.isArray(values.identityTag)){
        values.identityTag = values.identityTag.join(',')
      }
      if(values.tag.length && think.isArray(values.tag)){
        values.tag = values.tag.join(',')
      }

      if(values.city.length && values.city){
        const city_name = values.city.split(',')
        const citys = await this.model('region').where({region_id: ['IN', city_name]}).select();
        if(citys.length===2){
          values.city_name = citys[1].region_name
        }
      }

      await model.where({id: id}).update(values);
    } else {
      delete values.id;

      // const hasUser = await model.where({name: values.name, company_name: values.company_name}).find()
      // if(!think.isEmpty(hasUser)){
      //   return this.fail(400, '人物重复');
      // }
      
      if(values.identityTag.length && think.isArray(values.identityTag)){
        values.identityTag = values.identityTag.join(',')
      }

      if(values.city.length && values.city){
        const city_name = values.city.split(',')
        const citys = await this.model('region').where({region_id: ['IN', city_name]}).select();
        if(citys.length===2){
          values.city_name = citys[1].region_name
        }
      }

      let tagArr = []
      
      if(values.tag.length && think.isArray(values.tag)){
        tagArr = JSON.parse(JSON.stringify(values.tag))
        values.tag = values.tag.join(',')
      }

      if(values.phone || values.weixin){
        values.has_contact = 1
      }
      values.create_time = ['exp', 'CURRENT_TIMESTAMP()']

      await model.add(values);

      if(tagArr.length){
        for (const item of tagArr) {
          await this.model('tags').where({id: item}).increment('count', 1);
        }
      }
    }
    return this.success(values);
  }

  async updateAction(){
    const id = this.post('id');
    const statusVip = this.post('is_vip');
    const statusShow = this.post('show');
    const model = this.model('figure');
    if(!think.isTrueEmpty(statusVip)){
      await model.where({id: id}).update({
        is_vip: statusVip
      });
    }
    if(!think.isTrueEmpty(statusShow)){
      await model.where({id: id}).update({
        show: statusShow
      });
    }
    
    return this.success();
  }

  async deleteAction() {
    const id = this.get('id');
    await this.model('figure').where({id: id}).limit(1).delete();

    return this.success();
  }
  async figureShowMultiAction() {
    const ids = this.post('ids');
    await this.model('figure').where({id: ['IN', ids]}).update({
      show: 1
    });
    return this.success();
  }
};
